package com.fahai.web.service.impl;/**
 * Created by Lilinjun on 17/5/24.
 */

import com.fahai.common.PageVo;
import com.fahai.web.dao.CardInfoDao;
import com.fahai.web.dao.ProductInfoDao;
import com.fahai.web.dao.SkuInfoDao;
import com.fahai.web.entity.CardInfo;
import com.fahai.web.entity.ProductInfo;
import com.fahai.web.entity.SkuInfo;
import com.fahai.web.service.CardInfoService;
import com.fahai.web.vo.CardListVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 卡券套餐
 *
 * @author
 * @create 2017-05-24 下午4:05
 **/
@Service
@Transactional(rollbackFor = DataAccessException.class)
public class CardInfoServiceImpl implements CardInfoService {
    @Autowired
    CardInfoDao dao_card;
    @Autowired
    SkuInfoDao daosku;
    @Autowired
    ProductInfoDao daopro;

    @Override
    public SkuInfo selectSkuInfoById(Integer skuId) {
        return  daosku.selectByPrimaryKey(skuId);
    }

    @Override
    public CardInfo selectCardInfoById(Integer tcid) {
        return dao_card.selectByPrimaryKey(tcid);
    }

    @Override
    public int count(CardInfo cardListVo, PageVo pageVo) {
        int i= dao_card.selectCount(cardListVo);

        return  i;
    }

    @Override
    public PageVo findSceneList(CardListVo cardListVo, PageVo pageVo) {
        Map<String, Object> map = new HashMap<String, Object>();
        if(null!=cardListVo.getTctype() ){
            map.put("tctype",cardListVo.getTctype());
        }
        if(null!=cardListVo.getTcisopen() ){
            map.put("tcisopen",cardListVo.getTcisopen());
        }
        if(null!=cardListVo.getOrdertype()&&!cardListVo.getOrdertype().equals("")){
            map.put("ordertype",cardListVo.getOrdertype());
        }
        Page<CardListVo> page = PageHelper.startPage(pageVo.getPageNum(), pageVo.getPageSize());
        List<CardListVo> vos = dao_card.selectCardList(map);
        for (CardListVo vo:vos
             ) {
            //处理tcsn
//            if(vo.getTcSn()!=null){
//                if(vo.getTcSn().length()>3){
//                    vo.setBak(vo.getTcSn().substring(3,vo.getTcSn().length()).replace("-",""));
//                    vo.setTcSn(vo.getTcSn().substring(0,3));
//                }else{
//                    vo.setBak("");
//                    vo.setTcSn(vo.getTcSn().replace("-",""));
//                }
//            }
            //处理 面值 定价
            if(null!=vo.getTcRemark()&&!"".equals(vo.getTcRemark())) {
                try {
                    vo.setTcRemark(URLDecoder.decode(vo.getTcRemark(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

        }
        pageVo.setResult(vos);
        return pageVo;
    }

    /**
     * 更新card_info表中的卡券
     *
     * @param cardInfo
     */
    @Override
    public void update(CardInfo cardInfo) {
        dao_card.updateByPrimaryKeySelective(cardInfo);

    }

    /**
     * 更新card_info表中的卡券
     *
     * @param cardListVo
     */
    @Override
    public String check(CardListVo cardListVo) {
        String name=cardListVo.getLawyername();//
        String tel=cardListVo.getLawyerPhone();//
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("lname",name);
        map.put("tel",tel);
       String lsid= dao_card.check(map);
        return lsid;
    }

    /**
     * 更新sku_info中的卡券
     *
     * @param skuInfo
     */
    @Override
    public void update(SkuInfo skuInfo) {
        daosku.updateByPrimaryKeySelective(skuInfo);
    }

    /**
     * 增加card_info中的卡券
     *
     * @param cardInfo
     */
    @Override
    public void add(CardInfo cardInfo) {
        dao_card.insert(cardInfo);
    }

    /**
     * 增加sku_info中的卡券
     *
     * @param skuInfo
     */
    @Override
    public void add(SkuInfo skuInfo) {
        daosku.insert(skuInfo);
    }


    /**
     * 批量上架sku_info
     *
     * @param tcids
     */
    @Override
    public void batchOnSaleSku(List<Integer> tcids) {
       // Map<String, Object> map = new HashMap<String, Object>();
        dao_card.batchOnSalesku(tcids);
    }

    /**
     * 批量上架card_info
     *
     * @param tcids
     */
    @Override
    public void batchOnSaleCard(List<Integer> tcids) {
     //   Map<String, Object> map = new HashMap<String, Object>();
        dao_card.batchOnSalecard(tcids);
    }


    /**
     * 批量下架sku_info
     *
     * @param tcids
     */
    @Override
    public void batchDownSaleSku(List<Integer> tcids) {
      //  Map<String, Object> map = new HashMap<String, Object>();

        dao_card.batchDownSalesku(tcids);
    }

    /**
     * 批量下架card_info
     *
     * @param tcids
     */
    @Override
    public void batchDownSaleCard(List<Integer> tcids) {
      //  Map<String, Object> map = new HashMap<String, Object>();
        dao_card.batchDownSalecard(tcids);
    }

    @Override
    public PageVo findProductions(ProductInfo productInfo, PageVo pageVo) {
        pageVo.setResult(daopro.select(productInfo));
        return pageVo;
    }
}
